//
//  GoodsInfoVC.m
//  EJLX
//
//  Created by adc on 15/11/9.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "GoodsInfoVC.h"
#import "WoDeShangPinViewController.h"
#import "MyShopVC.h"
#import "ShoppingVC.h"
#import "AddGoodsToGouWuCarCell.h"
#import "ShoppingOfCartVC.h"
#import "GoodsinfoTableView.h"
@interface GoodsInfoVC ()

@end

@implementation GoodsInfoVC

-(void)getGoodsSection:(int)section Row:(int)row{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
     
    [GMDCircleLoader setOnView:self.view withTitle:nil animated:YES];

    
    [HttpsManager requestForDetailInfoWithProductID:self.productId completionBlock:^(GoodsDetailInfo *detail) {
        if (detail == nil) {
            LoginVC *vc = [[LoginVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            
            self.detail = detail;
            [self add];
            
        }
        
    }];
    [GMDCircleLoader hideFromView:self.view animated:YES];
    _numberOfGoodsForCar = 1;
    [self creatNavAndTabbar];
    
    // self.webForGoods = [[UIWebView alloc] initWithFrame:CGRectMake(0, 300, WIDE, HEIGHT-300)];
    
    self.webForGoods.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    // self.webForGoods.backgroundColor = [UIColor lightGrayColor];
    self.webForGoods.scalesPageToFit = YES;
    self.webForGoods.contentMode = UIViewContentModeRedraw;
    self.webForGoods.opaque = YES;
    [_webForGoods loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.nuomi.com"]]];
    [self.view addSubview:_webForGoods];
    
    // [self creatSmallView];
    [self performSelector:@selector(add) withObject:nil afterDelay:1.0];
     _currentYanSeIndex = 0;
    
}


-(void)creatNavAndTabbar{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    [leftBtn setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60, 7, 120, 30  )];
    title.text = @"商品详情";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [navView addSubview:title];
    
    
    //tabbar自定义还是系统的
    UIView *tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT-30,WIDE, 30)];
    tabbarView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tabbarView];
    
    NSArray *tabTitleArr = @[@"我的商品",@"上架",@"加入购物车"];
    for (int i =0 ; i<tabTitleArr.count; i++) {
        UIButton *tabbarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tabbarBtn.frame = CGRectMake((WIDE/6.f-30)+WIDE/3.f*i, 0,60 , 30);
        // tabbarBtn.layer.cornerRadius = 20;
        tabbarBtn.tag = i;
        tabbarBtn.titleLabel.font = [UIFont systemFontOfSize:12.0f];
        if (i == 1) {
            [tabbarBtn setTitle:@"已上架" forState:UIControlStateSelected];
        }
        [tabbarBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [tabbarBtn setTitle:tabTitleArr[i]  forState:UIControlStateNormal] ;
        [tabbarBtn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
        [tabbarView addSubview:tabbarBtn];
        UIView *small = [[UIView alloc] initWithFrame:CGRectMake(((CGFloat)WIDE/3) *i, 5, 1, 20)];
        small.backgroundColor = [UIColor lightGrayColor];
        [tabbarView addSubview:small];
        
    }
}

-(void)backBtn{
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)rigBtn{
    
    MyShopVC *shop = [[MyShopVC alloc] init];
    [self.navigationController pushViewController:shop animated:YES];
}
-(void)btn:(UIButton *)btn{
     
       if (btn.tag == 0) {
           JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
           [HUD showInView:self.view];
           

        WoDeShangPinViewController*woDe=[[WoDeShangPinViewController alloc]init];
        woDe.shopId=_detail.infoShopID;
        
        [self.navigationController pushViewController:woDe animated:YES];
        [HUD dismiss];
    }else if (btn.tag == 1){
        if (btn.selected) {
            JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
            [HUD showInView:self.view];
            HUD.useProgressIndicatorView = NO;
            HUD.textLabel.text = @"商品已上架，请勿再次上架";

            //SHOW_ALERT(@"商品已上架，请勿再次上架");
            [HUD dismissAfterDelay:1.0];

            
        }else{
            JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
            [HUD showInView:self.view];
            

            //上架
            NSMutableArray *arr = [NSMutableArray arrayWithObject:[NSString stringWithFormat:@"%d",self.productId]];
            [HttpsManager requestGetShangJiaShangPinProducts:arr requestUrl:@"add"  Competion:^(int errcode, NSString *errmsg) {
                //    [GMDCircleLoader setOnView:self.view withTitle:@"请稍后....." animated:YES];
                if (errcode == 0) {
                    
                    //    [GMDCircleLoader hideFromView:self.view animated:YES];
                   // SHOW_ALERT(@"商品上架成功")
                    HUD.useProgressIndicatorView = NO;
                    HUD.textLabel.text = @"商品上架成功";
                    [HUD dismiss];

                    btn.selected = !btn.selected;

                    
                }else{
                   // SHOW_ALERT(errmsg)
                    btn.selected = !btn.selected;
                    HUD.useProgressIndicatorView = NO;
                    HUD.textLabel.text = errmsg;
                    [HUD dismissAfterDelay:0.5];

                }
                
                
            }];
            
        }
    }else{
        
        [GMDCircleLoader setOnView:self.view withTitle:nil animated:YES];
        GoodsinfoTableView *table = [[GoodsinfoTableView alloc] init];
        table.productId = [NSString stringWithFormat:@"%d", self.productId];
        table.price =  [NSString stringWithFormat:@"价格：%.2f",(CGFloat) self.detail.newPrice*0.01 ];
        
        table.shopID = self.detail.infoShopID;
        self.definesPresentationContext = YES;
        table.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        table.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [self presentViewController:table animated:YES completion:nil];
        [GMDCircleLoader hideFromView:self.view animated:YES];
        table.yanAndChiBlock =^(NSString *str,NSString *yanse,NSString *chima,NSString *num,NSString *attr1,NSString*attr2){
            if ([str isEqualToString:@"OK"]) {
                
                ShoppingOfCartVC *car =[[ShoppingOfCartVC alloc] init];
                [self.navigationController pushViewController:car animated:YES];
                
            }
        };

    }
}
-(void)add{
    [self creatTopView];
    
    GoodsDetailInfo *good = self.detail;
    
    self.goodsName.text = [NSString stringWithFormat:@"  %@" ,good.title];
    self.goodsNewPrice.text = [NSString stringWithFormat:@"现价:¥%.2f",good.newPrice*0.01] ;
   
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"市价:¥%.2f",good.oldPrice*0.01]];
    [str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(3, str.length-3)];
    self.goodsMarketPrice.attributedText = str;
    
      self.goodsCommission.text =[NSString stringWithFormat:@"%d",self.detail.commison];
    float zhe = (float)self.detail.newPrice/(float)self.detail.oldPrice;
    
    NSMutableAttributedString *zhestr= [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"折扣:%.1f折",zhe*10]];
    [zhestr addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(3, zhestr.length-3)];
    self.goodsDiscountedPrice.attributedText = zhestr;
    self.goodsCommission.text = [NSString stringWithFormat:@"佣金:¥%.2f",self.detail.commison*0.01];
    self.postPrice.text= @"  邮费：包邮";
    
  
    self.goodsShouCang.text = [NSString stringWithFormat:@"收藏:%d",self.detail.num];
}
-(void)btnClick:(UIButton *)btn{
    
}
-(void)creatTopView{
    
    _scrollView.contentSize = CGSizeMake(WIDE*self.detail.detailImgUrlArray.count, 100);
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    // if (self.detail.detailImgUrlArray.count>0) {
    
    for (int i = 0 ; i < self.detail.detailImgUrlArray.count; i ++) {
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(WIDE*i, 0, WIDE, 100)];
        [img sd_setImageWithURL:self.detail.detailImgUrlArray[i]];
        img.backgroundColor = [UIColor lightGrayColor];
        [_scrollView addSubview:img];
    }
    _pageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 200, WIDE, 20)];
    _pageControll.numberOfPages = self.detail.detailImgUrlArray.count;
    _pageControll.enabled =YES;
    _pageControll.pageIndicatorTintColor = [UIColor lightGrayColor];
    _pageControll.currentPageIndicatorTintColor = [UIColor greenColor];
    [self.view addSubview:_pageControll];
    // }
    
    
    
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _pageControll.currentPage = scrollView.contentOffset.x/WIDE;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
